package com.elitel.frame.base.controller;

import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elitel.frame.base.service.SysLoginfoService;
import com.elitel.frame.main.entity.SysLoginfo;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;


/**
 * @Description 日志监控
 * @author guoyanfei
 */
@Controller
@RequestMapping("/system/sysloginfo")
public class SysLoginfoController {
	@Autowired
	private SysLoginfoService sysLoginfoService;
	

	private static final Logger logger = LoggerFactory.getLogger(DataConfigController.class);
	
	@RequestMapping("/pageList")
	@ResponseBody
	public PageResponseVo<SysLoginfo> getListByPage(HttpServletRequest request){
		PageResponseVo<SysLoginfo> sysloginfos = new PageResponseVo<SysLoginfo>();
		try {
			PageRequestVo<SysLoginfo> pr = new PageRequestVo<SysLoginfo>();
			int pageNo = Integer.valueOf(request.getParameter("pageNo")).intValue();
			int pageSize = Integer.valueOf(request.getParameter("pageSize")).intValue(); 
			String seachTxt = request.getParameter("seachTxt");
			pr.setSearchContent(seachTxt);
			pr.setPageNo(pageNo);
			pr.setPageSize(pageSize);
			sysloginfos = sysLoginfoService.getListByPage(pr);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return sysloginfos;
	}
	
	@RequestMapping("/realTimeLoginfo")
	@ResponseBody
	public String realTimeLoginfo(){
		StringBuffer strtxt = new StringBuffer();
//        try{
//        	Integer i =0;
//        	while (true) {
//        		i++;
//        		if(i<=5){
//        			java.lang.Object text = amqpTemplate.receiveAndConvert("logQueueDestination");
////	        		LoggingEvent logcontent = (LoggingEvent) jmsTemplate.receiveAndConvert(destination);
//					if(text !=null){
//						LoggingEvent logcontent = (LoggingEvent)text;
//						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//						strtxt.append("<p>====日志Begin(异常时间:"+dateFormat.format(logcontent.getTimeStamp())+")====<br/>");
//						strtxt.append("队列："+"logQueueDestination"+",内容：");
//						strtxt.append("异常信息:"+logcontent.getRenderedMessage());
//						strtxt.append(",异常位置:"+logcontent.getLocationInformation().fullInfo);
//						strtxt.append("<br/>===日志End===<p>");
//					}else{
//						break;
//					}
//        		}else{
//					break;
//				}
//			}
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return strtxt.toString();
    }
	
	@RequestMapping("/cleanAllLoginfo")
	@ResponseBody
	public void cleanAllLoginfo(){
		sysLoginfoService.cleanAllData();
	}
	
	@RequestMapping("/deleteLoginfoByguid")
	@ResponseBody
	public Integer deleteLoginfoByguid(HttpServletRequest request){
		int result = 0;
		try {
			String guids = request.getParameter("guids");
			result = sysLoginfoService.deleteDataByguids(guids);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
}
